package com.lyj.sc.aliyuntianchi.test20220715;

/**
 * @program: code-random
 * @description:
 * @author: lyj
 * @create: 2022-07-15 09:48
 * @version: 1.0
 * @Modifier By:
 **/
  class ListNode {
      int val;
      ListNode next;
      ListNode(int x) {
          val = x;
          next = null;

    }
 }
public class test2 {
    public boolean hasCycle(ListNode head) {
        //双指针
        ListNode slow = head;
        ListNode fast = head;
        while (slow.next!=null && slow.next.next!=null){
            slow=slow.next;
            fast=fast.next.next;
            if(slow==fast){
                break;
            }
        }
        if(slow.next==null){
            return false;
        }
        slow=head;
        while (slow!=fast){
            slow=slow.next;
            fast=fast.next;
        }
        return true;
    }
}
